/*
 * @lc app=leetcode.cn id=434 lang=typescript
 *
 * [434] 字符串中的单词数
 */

// @lc code=start
function countSegments(s: string): number {
    let count = 0;
    let cache = 0;
    for (let index = 0; index < s.length; index++) {
        const element = s[index];
        if (element === " ") {
            if (cache > 0) {
                count++;
                cache = 0;
            }
        } else {
            cache += 1;
        }
    }
    return cache ? count + 1 : count;
}
// @lc code=end
